package com.example.SocialApplication.activity.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.Toast;

import com.example.SocialApplication.Daemon;
import com.example.SocialApplication.R;
import com.example.SocialApplication.adapters.PeoplesAdapter;
import com.example.SocialApplication.db.entity.User;
import com.example.SocialApplication.service.CallServiceGetFacebookUsers;
import com.example.SocialApplication.service.CallServiceGetVkUsers;

import java.util.List;

/**
 * Created by differ on 25.08.13.
 */
public class PeopleFragment extends Fragment {

    private GridView mPeopleGrid;
    private List<User> mListUsers;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.people_fragment, container, false);
        mPeopleGrid = (GridView) rootView.findViewById(R.id.people_fragment_grid_view);

        String login = Daemon.get("login", String.class);

        if (login.equals("vk")) {
            new CallServiceGetVkUsers() {
                @Override
                protected void onPostExecute(List<User> users) {
                    mListUsers = users;
                    if (mListUsers != null)
                        loadUsers();
                }
            }.execute();
        } else if (login.equals("facebook")) {
            new CallServiceGetFacebookUsers() {
                @Override
                protected void onPostExecute(List<User> users) {
                    mListUsers = users;
                    if(mListUsers != null)
                        loadUsers();
                }
            }.execute();
        } else {
            Toast.makeText(getActivity(), "FAILED LOAD PEOPLE LIST", Toast.LENGTH_SHORT).show();
        }

        return rootView;
    }

    private void loadUsers() {
        PeoplesAdapter adapter = new PeoplesAdapter(getActivity(), R.layout.people_grid_item, mListUsers);
        mPeopleGrid.setAdapter(adapter);
    }
}
